<?php
declare (strict_types = 1);

namespace app\api\controller;

use think\exception\ValidateException;

use app\common\model\{CollegeTeacher, Hall, HallSign, User, UserNews, UserSeek, CollegeCourse, Video};
use app\common\validate\{TeacherValidate, SeekValidate, CourseValidate};

class CollegeController extends BaseController
{
    /** 学院师资列表 */
    public function teacher_list()
    {
        $data = input('');

        try{
            validate(TeacherValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $teacherModel = new CollegeTeacher();

        $field = 'id, name, head_img';

        /** 查询师资列表 */
        $list = $teacherModel->getList([], 1, $data['page'], $data['limit'], $field);

        if($list['code'] != 'ok'){
            $this->error_back($list['msg']);
        }

        $this->success_back('ok', $list['data']);
    }

    /** 课程列表 */
    public function course_list()
    {
        $data = input('');

        try{
            validate(CourseValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $userModel   = new User();
        $courseModel = new CollegeCourse();

        $field = 'id, user_id, title, head_img, price';

        /** 查询课程列表 */
        $list = $courseModel->getList([], 1, $data['page'], $data['limit'], $field);

        if($list['code'] != 'ok'){
            $this->error_back($list['msg']);
        }

        foreach($list['data'] as $v){
            $v['user_info'] = $userModel->getFind(['id' => $v['user_id']], 'head_img, nickname')['data'];
        }

        $this->success_back('ok', $list['data']);
    }

    /** 课程详情 */
    public function course_show()
    {
        $data = input('');

        try{
            validate(CourseValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $userModel   = new User();
        $videoModel  = new Video();
        $courseModel = new CollegeCourse();

        $field = 'id, user_id, title, price, brief';
        $where = ['id' => $data['course_id']];

        /** 查询课程信息 */
        $info = $courseModel->getFind($where, $field);

        if($info['code'] != 'ok'){
            $this->error_back($info['msg']);
        }

        $info['data']['video'] = $videoModel->getCol(['obj_id' => $data['course_id']], 'url')['data'];
        $info['data']['user_info'] = $userModel->getFind(['id' => $info['data']['user_id']], 'head_img, nickname, fans')['data'];

        $this->success_back('ok', $info['data']);
    }

    /** 课堂列表 */
    public function hall_list()
    {
        $data = input('');

        try{
            validate(TeacherValidate::class)->scene('hall_list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $hallModel = new Hall();
        $signModel = new HallSign();

        /** 查询堂口信息 */
        $field = 'id, head_img, title, price';
        $where[] = ['type', '=', $data['type']];
        $where[] = ['status', '=', $data['status']];
        
        $list = $hallModel->getList($where, 1, $data['page'], $data['limit'], $field);

        if($list['code'] != 'ok'){
            $this->error_back($list['msg']);
        }

        $this->success_back('ok', $list['data']);
    }

    /** 课堂详情 */
    public function hall_show()
    {
        $data = input('');

        try{
            validate(TeacherValidate::class)->scene('hall_show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $hallModel = new Hall();
        $signModel = new HallSign();

        /** 查询堂口信息 */
        $field = 'id, head_img, title, price, start_time, end_time, address, target, target_decode, course, learn_status, sign_ask, sign_money';
        $where = ['id' => $data['hall_id']];

        $info = $hallModel->getFind($where, $field);

        if($info['code'] != 'ok'){
            $this->error_back($info['msg']);
        }

        /** 查询当前用户是否已报名 */
        $sign_flag = $signModel->getFind(['user_id' => $data['user_id'], 'hall_id' => $info['data']['id'], 'status' => 2], 'id');

        if($sign_flag['code'] != 'ok'){
            $info['data']['user_sign'] = 0;
        }else{
            $info['data']['user_sign'] = 1;
        }

        $this->success_back('ok', $info['data']);
    }

    /** 课堂报名 */
    public function hall_sign()
    {
        $data = input('');

        try{
            validate(TeacherValidate::class)->scene('sign')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $signModel = new HallSign();

        /** 新增报名 */
        $data['order_no'] = getOrderNo('TK');
        $data['status'] = 2;
        $flag = $signModel->add($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('报名成功', $flag['data']);
    }

    /** 学员库 */
    public function student()
    {
        $data = input('');

        try{
            validate(TeacherValidate::class)->scene('student')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $userModel = new User();
        $newsModel = new UserNews();
        $signModel = new HallSign();

        $field = 'id, user_id';
        $where[] = ['user_id', '<>', $data['user_id']];
        $where[] = ['hall_id', '=', $data['hall_id']];
        $where[] = ['status', '=', 2];
        
        $list = $signModel->getList($where, 1, $data['page'], $data['limit'], $field);

        if($list['code'] != 'ok'){
            $this->error_back($list['msg']);
        }

        $user_list = [];
        foreach($list['data'] as $v){
            $user_info = $userModel->getFind(['id' => $v['user_id']], 'head_img, nickname');
            $news_info = $newsModel->getFind(['user_id' => $v['user_id']], 'birthday, edu_status, job_type, address_seat');

            $list_info = [
                'id'           => $v['user_id'],
                'head_img'     => $user_info['data']['head_img'],
                'nickname'     => $user_info['data']['nickname'],
                'age'          => howOld($news_info['data']['birthday']),
                'edu_status'   => $news_info['data']['edu_status'],
                'job_type'     => $news_info['data']['job_type'],
                'address_seat' => $news_info['data']['address_seat'],
            ];

            $user_list[] = $list_info;
        }

        $this->success_back('ok', $user_list);
    }

    /** 追求||取消追求 */
    public function set_seek()
    {
        $data = input('');

        try{
            validate(SeekValidate::class)->scene('set')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $seekModel = new UserSeek();

        $where = ['user_id' => $data['user_id'], 'to_user_id' => $data['to_user_id']];
        $info = $seekModel->getFind($where);

        if($info['code'] != 'ok'){
            $flag = $seekModel->add($data);
            $field = '追求';
        }else{
            $flag = $seekModel->del($where);
            $field = '取消追求';
        }

        if($flag['code'] != 'ok'){
            $this->error_back($field . '失败');
        }

        $this->success_back($field . '成功');
    }
}